package cn.edu.hbwe.book.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import cn.edu.hbwe.book.entity.User;
import cn.edu.hbwe.book.utils.C3P0Util;

public class UserDao {
	private static List<User> ulist = new ArrayList<User>();
	private static QueryRunner run = new QueryRunner(C3P0Util.getDataSource());

	// 添加用户
	public void addUser(User u) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "insert into user(username,password,gender,email,telephone,description,activeCode,role,state,regisTime)"
				+ "values(?,?,?,?,?,?,?,?,?,?)";

		int row = run.update(sql, u.getUsername(), u.getPassword(), u.getGender(), u.getEmail(), u.getTelephone(),
				u.getDescription(), u.getActiveCode(), "普通用户",1, u.getRegisTime());
		if (row == 0) {
			throw new RuntimeException();
		}
	}

	public User findByUnameAndPword(String username, String password) throws SQLException {
		String sql = "select * from user where username=? and password=?";
		User user = run.query(sql, new BeanHandler<User>(User.class), username, password);
		return user;
	}

	public void updateUserinfo(User user) throws SQLException {
		String sql = "update user set password=?,gender=?,telephone=? where username=?";
		run.update(sql,user.getPassword(),user.getGender(),user.getTelephone(),user.getUsername());	
	}

}
